package com.shouer.doc.ui.qrcode.presenter;

import android.graphics.Bitmap;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.application.response.QueryUserResponse;
import com.shouer.doc.common.util.BitmapUtil;
import com.shouer.doc.common.util.QRCodeUtil;
import com.shouer.doc.common.util.ThreadPoolUtil;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.qrcode.response.CreateQRImgeResponse;
import com.shouer.doc.ui.qrcode.view.IQRCodeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY-Work on 2017/10/23.
 */

public class QRCodePresenterImpl implements IQRCodePresenter{


    protected IQRCodeView qrCodeView;
    public QRCodePresenterImpl(IQRCodeView qrCodeView)
    {
        this.qrCodeView=qrCodeView;
        EventBus.getDefault().register(this);
    }

    public void onDestroy()
    {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void queryQRCodeInfo(String userId) {

        ShouerLoadingDialogUtil.getInstance().show(qrCodeView.getContext());
        Map<String, String> param = new HashMap<>();
        param.put("userId", userId);

        final GsonRequest<QueryUserResponse> request = new GsonRequest("queryUser", QueryUserResponse.class, new Response.Listener<QueryUserResponse>() {
            @Override
            public void onResponse(QueryUserResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                QueryUserResponse response=new QueryUserResponse();
                response.setErrorMsg("网络异常 查询二维码失败");
                response.setErrorCode(-1);
                EventBus.getDefault().post(response);

            }
        });

        RequestUtil.sendRequest(qrCodeView.getContext(), param, request);
    }

    @Override
    public void createQRImage(final String context, final int widthPix, final int heightPix, final int logoResId, final String filePath) {
        ShouerLoadingDialogUtil.getInstance().show(qrCodeView.getContext());
        ThreadPoolUtil.getInstance().getCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                Bitmap logoBm = BitmapUtil.drawableToBitmap(qrCodeView.getContext().getResources().getDrawable(logoResId));
                boolean result = QRCodeUtil.createQRImage(context, widthPix, heightPix, logoBm, filePath);
                if (!logoBm.isRecycled()) {
                    logoBm.recycle();
                    logoBm = null;
                }

                if (result) {
                    CreateQRImgeResponse response=new CreateQRImgeResponse();
                    response.setErrorCode(0);
                    response.setQrFilePath(filePath);
                    EventBus.getDefault().post(response);
                } else
                {
                    CreateQRImgeResponse response=new CreateQRImgeResponse();
                    response.setErrorCode(-1);
                    response.setErrorMsg("创建二维码失败");
                    EventBus.getDefault().post(response);
                }

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CreateQRImgeResponse response)
    {
        if (response.getErrorCode()>=0)
        {
            qrCodeView.createQrImageSuccess(response.getQrFilePath());
        }else
        {
            qrCodeView.createQrImageFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryUserResponse response)
    {
        if (response.getErrorCode()>=0)
        {
            if (null!=response.getDoctor())
            {
                qrCodeView.queryQrImageSuccess(response.getDoctor().getDocShareUrl(),response.getDoctor().getDocShareTip());
            }else
            {
                qrCodeView.queryQrImageSuccess(null,null);
            }
        }else
        {
            qrCodeView.queryQrImageFailed(response.getErrorMsg());
        }

    }
}
